****APPENDIX FIGURE C7-C8****

cd "$main_dir/output/figures/appendix_c"

graph set window fontface "Palatino"

global basic 	sex marital religion edu

//--------------------------------------------------------
//Generate relevant program
//--------------------------------------------------------

capture program drop standardize
program define standardize
	
	args depvar
	
	cap drop z*
	local varlist `depvar' wgrowth_rate_1 yob_ln_gdppc
	foreach var of local varlist {
		egen z`var' = std(`var') if e(sample)==1
}														

end

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc)

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.

//--------------------------------------------------------
//Create Figure C7
//--------------------------------------------------------

matrix coef1 = J(31, 3, .)
	matrix coln coef1 = coefficients lcl ucl
	matrix rown coef1 = "1990" "1991" "1995" "1996" "1997" "1998" "1999" ///
						"2000" "2001" "2002" "2003" "2004" "2005" "2006" ///
						"2007" "2008" "2009" "2010" "2011" "2012" "2013" ///
						"2014" "2015" "2016" "2017" "2018" "2019" "2020" ///
						"2021" "2022" "2023"

local i = 1
foreach x in ///
	1990 1991 1995 ///
	1996 1997 1998 1999 2000 ///
	2001 2002 2003 2004 2005 ///
	2006 2007 2008 2009 2010 ///
	2011 2012 2013 2014 2015 ///
	2016 2017 2018 2019 2020 ///
	2021 2022 2023 ///
	{
		qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & survey_year!=`x', absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
		standardize trust_govt
		reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & survey_year!=`x', absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
		
		mat os = r(table)
		mat coef1[`i',1] = os[1,1]
		mat coef1[`i',2] = os[5,1]
		mat coef1[`i',3] = os[6,1]
		
		di "Regression for year `x'"
		local ++i
	}

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
standardize trust_govt
qui reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
scalar beta = _b[zwgrowth_rate_1]
coefplot (matrix(coef1[,1]), ci((coef1[,2] coef1[,3]))), ///
	graphregion(color(white)) bgcolor(white) xline(0, lcolor(maroon)) xlabel(-0.025(0.025)0.06,labs(vsmall)) ylabel(,labs(vsmall)) xline(`=beta', lpattern(dash) lcolor(maroon)) ///
	ytitle("Survey Year Dropped",size(small) margin(small)) xtitle("Effect Size", size(small))
	graph export "figure_c7.png", replace

//--------------------------------------------------------
//Create Figure C8
//--------------------------------------------------------

matrix coef2 = J(14, 3, .)
	matrix coln coef2 = coefficients lcl ucl
	matrix rown coef2 = "Australia and New Zealand" "Central Asia" "Eastern Asia" "Eastern Europe" "Latin America and the Caribbean" "Northern Africa" "Northern America" "Northern Europe" "South-eastern Asia" "Southern Asia" "Southern Europe" "Sub-Saharan Africa" "Western Asia" "Western Europe"
	
qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Australia and New Zealand", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Australia and New Zealand", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[1,1] = os[1,1]
	mat coef2[1,2] = os[5,1]
	mat coef2[1,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Central Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Central Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[2,1] = os[1,1]
	mat coef2[2,2] = os[5,1]
	mat coef2[2,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Eastern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Eastern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[3,1] = os[1,1]
	mat coef2[3,2] = os[5,1]
	mat coef2[3,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Eastern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Eastern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[4,1] = os[1,1]
	mat coef2[4,2] = os[5,1]
	mat coef2[4,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Latin America and the Caribbean", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Latin America and the Caribbean", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[5,1] = os[1,1]
	mat coef2[5,2] = os[5,1]
	mat coef2[5,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Northern Africa", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Northern Africa", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[6,1] = os[1,1]
	mat coef2[6,2] = os[5,1]
	mat coef2[6,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Northern America", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Northern America", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[7,1] = os[1,1]
	mat coef2[7,2] = os[5,1]
	mat coef2[7,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Northern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Northern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[8,1] = os[1,1]
	mat coef2[8,2] = os[5,1]
	mat coef2[8,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="South-eastern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="South-eastern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[9,1] = os[1,1]
	mat coef2[9,2] = os[5,1]
	mat coef2[9,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Southern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Southern Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[10,1] = os[1,1]
	mat coef2[10,2] = os[5,1]
	mat coef2[10,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Southern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Southern Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[11,1] = os[1,1]
	mat coef2[11,2] = os[5,1]
	mat coef2[11,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Sub-Saharan Africa", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Sub-Saharan Africa", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[12,1] = os[1,1]
	mat coef2[12,2] = os[5,1]
	mat coef2[12,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Western Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Western Asia", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[13,1] = os[1,1]
	mat coef2[13,2] = os[5,1]
	mat coef2[13,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1 & subregion!="Western Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
standardize trust_govt
reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1 & subregion!="Western Europe", absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode) level(95)
mat os = r(table)
	mat coef2[14,1] = os[1,1]
	mat coef2[14,2] = os[5,1]
	mat coef2[14,3] = os[6,1]

qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
standardize trust_govt
qui reghdfe ztrust_govt zwgrowth_rate_1 zyob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
scalar beta = _b[zwgrowth_rate_1]	
coefplot (matrix(coef2[,1]), ci((coef2[,2] coef2[,3]))), ///
	graphregion(color(white)) bgcolor(white) xline(0, lcolor(maroon)) xlabel(-0.025(0.025)0.05,labs(vsmall)) ylabel(,labs(vsmall)) xline(`=beta', lpattern(dash) lcolor(maroon)) ///
	ytitle("Subcontinent Dropped",size(small) margin(small)) xtitle("Effect Size", size(small))
	graph export "figure_c8.png", replace
	
